package com.leicx.guli.search.controller;

import com.leicx.guli.common.enums.GuliExceptionEnum;
import com.leicx.guli.common.to.es.SkuEsModel;
import com.leicx.guli.common.utils.R;
import com.leicx.guli.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author daxiong
 * @date 2021/7/26 下午8:16
 */
@RequestMapping("/search/save")
@RestController
@Slf4j
public class ElasticSaveController {

    @Resource
    ProductSaveService productSaveService;


    @PostMapping("product")
    public R productStatusUp(@RequestBody List<SkuEsModel> esModels) {
        boolean success;
        try {
            success = productSaveService.productStatusUp(esModels);
        } catch (IOException e) {
            log.error(e.getMessage());
            return R.error(GuliExceptionEnum.PRODUCT_UP_ERROR);
        }
        if (success) {
            return R.ok();
        } else {
            return R.error(GuliExceptionEnum.PRODUCT_UP_ERROR);
        }
    }
}
